package com.training.dao.impl.xixiaoming;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.training.dao.xixiaoming.motorDao;
import com.training.pojo.chengshu.MotorRoom;

public class motorDaoImpl extends HibernateDaoSupport implements motorDao{

	public List<MotorRoom> listMotor(Integer start,Integer max) {
		System.out.println("listMotorDao");
	    Query query=this.getSession().createQuery("from MotorRoom where isdelete=0"); 
	    query.setFirstResult(start-1); 
	    query.setMaxResults(max); 
	    return query.list(); 

	}

	public Integer getRowCount() {
		String hql="select count(*) from MotorRoom where isdelete=0";
		List<Long> list=this.getHibernateTemplate().find(hql);
		return Integer.parseInt(list.get(0).toString());
	}

	public MotorRoom getMotorById(Integer id) {
		System.out.println("getMotorByIdDao");
		String hql="from MotorRoom where isdelete=0 and motid=?";
		List<MotorRoom> list=this.getHibernateTemplate().find(hql,id);
		MotorRoom motor=null;
		if(list!=null){
			motor=list.get(0);
		}
		return motor;
	}

	public void saveEditMotor(MotorRoom motor) {
		System.out.println("saveEditMotorDao");
		//motor.setIsdelete(0);
		this.getHibernateTemplate().saveOrUpdate(motor);
	}

	public void deleMotor(Integer id) {
		System.out.println("deleMotorDao");
		String hql="update MotorRoom set isdelete=1 where motid=?";
		this.getHibernateTemplate().bulkUpdate(hql,id);
	}

	public List<MotorRoom> serachMotor(Integer uid, Integer deptid,
			String deptname, String address) {
		StringBuilder hql=new StringBuilder("from MotorRoom where isdelete=0");
		if(uid!=0){
			hql.append(" and UUser.uid="+uid+" ");
		}
		if(deptid!=0){
			hql.append(" and UDept.deptid="+deptid+" ");
		}
		if(deptname!=null && (!deptname.equals(""))){
			hql.append(" and motname like '%"+deptname+"%' ");
		}
		if(address!=null && (!address.equals(""))){
			hql.append(" and motaddr like '%"+address+"%' ");
		}
		System.out.println(hql.toString());
		Query query = this.getSession().createQuery(hql.toString());
		return query.list();
	}

	public boolean checkMotorname(String motorname) {
		String hql="from MotorRoom where motname=?";
		List list=this.getHibernateTemplate().find(hql,motorname);
		System.out.println(list.size());
		if(list.size()==0){
			System.out.println("true");
			return true;
		}else{
			System.out.println("false");
			return false;
		}
	}
}
